#ifndef CHARDIAGRAM_HH
#define CHARDIAGRAM_HH

#include <QGraphicsView>
#include <QGraphicsEllipseItem>
#include <QTimer>

namespace dsa
{


class ChartPie;
class Node;
class ChartCenter : public QObject,
                    public QGraphicsEllipseItem
{
  Q_OBJECT
private:
  float _radius;
  float _realRadius;

  bool  _active;

protected:
  void hoverEnterEvent(QGraphicsSceneHoverEvent *event);
  void hoverLeaveEvent(QGraphicsSceneHoverEvent *event);

  void mousePressEvent(QGraphicsSceneMouseEvent *event);
  void mouseReleaseEvent(QGraphicsSceneMouseEvent *event);

public:
  ChartCenter (float radius);

  void UpdateCenter ();

signals:
  void Clicked ();

};

class ChartPie : public QObject,
                 public QGraphicsEllipseItem
{
  Q_OBJECT
private:
  QColor          _color;
  dsa::ChartPie   *_nextSibling;
  dsa::ChartPie   *_prevSibling;
  QTimer          _timer;

  float           _radius;
  float           _destRadius;
  float           _realRadius;

  float           _angle;
  float           _startAngle;
  float           _realAngle;

  float           _radiusSpeed;

  bool            _inUpdate;

  dsa::Node *_node;


  bool            _active;


private slots:
  void Timeout ();

  void EnableHover (bool enable);

protected:
  void hoverEnterEvent(QGraphicsSceneHoverEvent *event);
  void hoverLeaveEvent(QGraphicsSceneHoverEvent *event);

  void mousePressEvent(QGraphicsSceneMouseEvent *event);
  void mouseReleaseEvent(QGraphicsSceneMouseEvent *event);

public:
  ChartPie (float radius, dsa::ChartPie* prevSibling, float angle);

  void SetNode (dsa::Node* node);
  dsa::Node *GetNode () const;

  void SetAngle (float start, float angle);
  float GetAngle () const;
  float GetStartAngle () const;
  float GetRealAngel () const;

  void SetInUpdate (bool inUpdate);

  dsa::ChartPie* GetNext () const;
  dsa::ChartPie* GetPrev () const;

  void UpdatePie ();

signals:
  void Clicked ();

};


class ChartDiagram : public QGraphicsView
{
  Q_OBJECT
private:
  QGraphicsScene  *_scene;
  QTimer          _timer;

  dsa::ChartPie   *_firstPie;
  dsa::ChartPie   *_lastPie;
  dsa::ChartCenter  *_center;

  float           _speed;

  void SetInUpdate (bool inUpdate);
  bool            _isUp;

  dsa::Node         *_node;
  QList<dsa::Node*> _nodes;

  void UpdateChartsAndBuildUp();
  void BuildDown ();
  void BuildUp ();


private slots:
  void PieClicked ();
  void CenterClicker ();
  void UpdateBuildUp ();
  void UpdateBuildDown ();

public:
  ChartDiagram(QWidget* parent = 0);

  void Set (dsa::Node* node, QList<dsa::Node*> nodes);

signals:
  void ChangeDirectory (dsa::Node* node);
};

}

#endif // CHARDIAGRAM_HH
